//
//  MineViewController.m
//  test
//
//  Created by kepuG on 17/1/20.
//  Copyright © 2017年 text. All rights reserved.
//

#import "MineViewController.h"
#import "testViewController.h"

@interface MineViewController () <UIPageViewControllerDelegate ,UIPageViewControllerDataSource>

@property (strong ,nonatomic) NSMutableArray *arr;

@property (strong ,nonatomic) NSMutableArray *pageContent;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:11] forKey: UIPageViewControllerOptionSpineLocationKey];
    
    UIPageViewController *pagVC = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
    pagVC.delegate = self;
    pagVC.dataSource = self;
    self.arr = [NSMutableArray array];
    self.pageContent = [NSMutableArray array];
    
    for ( int i = 0; i<10; i++) {
        UIViewController *vc = [[UIViewController alloc] init];
        vc.view.backgroundColor = [UIColor cyanColor];
        //[self.arr addObject:vc];
        [self.pageContent addObject:[NSString stringWithFormat:@"%d",i]];
    }
    
    testViewController *first = [self viewControllerAtIndex:0];
    NSArray *controllers = [NSArray arrayWithObject:first];
    
    [pagVC setViewControllers:controllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
    }];
    
    pagVC.view.frame = CGRectMake(0, 84, kScreen_Width, 150);
    pagVC.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:pagVC.view];
    [self addChildViewController:pagVC];
    // Do any additional setup after loading the view.
}
// 得到相应的VC对象
- (testViewController *)viewControllerAtIndex:(NSUInteger)index {
    if (([self.pageContent count] == 0) || (index >= [self.pageContent count])) {
        return nil;
    }
    // 创建一个新的控制器类，并且分配给相应的数据
    testViewController *dataViewController =[[testViewController alloc] init];
    
    dataViewController.view.backgroundColor = [UIColor grayColor];
    dataViewController.data =[self.pageContent objectAtIndex:index];
    return dataViewController;
}


- (NSUInteger)indexOfViewController:(testViewController *)viewController {
    return [self.pageContent indexOfObject:viewController.data];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    
    NSUInteger index = [self indexOfViewController:(testViewController *)viewController];
    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }
    index--;
    // 返回的ViewController，将被添加到相应的UIPageViewController对象上。
    // UIPageViewController对象会根据UIPageViewControllerDataSource协议方法，自动来维护次序。
    // 不用我们去操心每个ViewController的顺序问题。
    return [self viewControllerAtIndex:index];
}

// 返回下一个ViewController对象
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    
    NSUInteger index = [self indexOfViewController:(testViewController *)viewController];
    if (index == NSNotFound) {
        return nil;
    }
    
    index++;
    if (index == [self.pageContent count]) {
        return nil;
    }
    return [self viewControllerAtIndex:index];
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
    DebugLog(@"==%@",previousViewControllers);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    testViewController *nest = [[testViewController alloc] init];
    [self.navigationController pushViewController:nest animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
